package com.kezesong.leasing.controller.admin;


import com.kezesong.leasing.common.Constant;
import com.kezesong.leasing.common.CookieUtils;
import com.kezesong.leasing.common.ResponseResult;
import com.kezesong.leasing.controller.BaseController;
import com.kezesong.leasing.controller.request.admin.AdminLoginReqBody;
import com.kezesong.leasing.controller.response.admin.AdminUserRspBody;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/admin")
public class AdminUserManagerController extends BaseController {

    @Value("${domainName}")
    private String domainName;

    @PostMapping("/adminLogin.do")
    @ApiOperation(value = "adminLogin.do", notes = "管理员登陆")
    public ResponseResult<AdminUserRspBody> adminLogin(@RequestBody AdminLoginReqBody requestBody,  HttpServletResponse httpServletResponse){
        AdminUserRspBody adminUserRspBody = new AdminUserRspBody();
        String userName = requestBody.getUserName();
        String password = requestBody.getUserPassword();
        if ("facai".equals(userName) && "facai12345678".equals(password)){
            adminUserRspBody.setUserName(userName);
            adminUserRspBody.setToken("testAdminToken");
            ResponseResult<AdminUserRspBody> integerResponseResult = buildSucessResponse(adminUserRspBody);
            CookieUtils.WriteCookie(Constant.ADMIN_USER_COOKIE_TOKEN, userName, httpServletResponse,domainName);
            return integerResponseResult;
        }else {
            ResponseResult integerResponseResult = buildFailedResponse();
            return integerResponseResult;
        }
    }

}
